# views/auth_views.py
from PySide6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel
from PySide6.QtCore import Qt

class LoginPage(QWidget):
    def __init__(self, event_bus, page_manager):
        super().__init__()
        self.event_bus = event_bus
        self.page_manager = page_manager
        self.init_ui()

    def init_ui(self):
        # 全屏设置
        self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint)
        self.setFixedSize(self.page_manager.main_window.size())  # 铺满主窗口

        layout = QVBoxLayout(self)
        layout.addWidget(QLabel("请登录", alignment=Qt.AlignCenter))
        login_btn = QPushButton("登录")
        login_btn.clicked.connect(self.on_login_success)
        layout.addWidget(login_btn, alignment=Qt.AlignCenter)

    def on_login_success(self):
        """登录成功后触发：销毁登录栈，根据是否首次启动进入配置栈或主栈"""
        # 模拟检查是否首次启动（实际项目中可读取本地配置）
        is_first_launch = True  # 首次启动为True，否则为False

        # 销毁登录栈
        self.page_manager.clear_stack(self.page_manager.auth_stack)

        if is_first_launch:
            # 进入配置栈（添加第一个配置页面）
            from views.config_views import ConfigPage1
            self.page_manager.push_page(
                self.page_manager.config_stack,
                ConfigPage1(self.event_bus, self.page_manager)
            )
            self.page_manager.switch_stack(self.page_manager.config_stack)
        else:
            # 直接进入主栈
            self.init_main_stack()

    def init_main_stack(self):
        """初始化主栈（首页等核心页面）"""
        from views.home_views import HomeMain
        self.page_manager.push_page(
            self.page_manager.main_stack,
            HomeMain(self.event_bus)
        )
        self.page_manager.switch_stack(self.page_manager.main_stack)